package io.bluefw.microapp.auth.resource;

import java.util.List;

import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import io.bluefw.microapp.auth.domain.Role;
import io.bluefw.microapp.auth.repository.RoleDAO;
import io.dropwizard.hibernate.UnitOfWork;
import io.dropwizard.jersey.params.LongParam;

@Path("/auth/role")
@Produces(MediaType.APPLICATION_JSON)
public class RoleResource {
    private final RoleDAO roleDAO;

    public RoleResource(RoleDAO roleDAO) {
        this.roleDAO = roleDAO;
    }
    
    @POST    
    @UnitOfWork
    public Role create(Role role) {
    	return roleDAO.persist(role);
    }
    
    @PUT  
    @UnitOfWork
    public void update(Role role) {
    	roleDAO.persist(role);
    }
    
    @GET
    @Path("{id}")   
    @UnitOfWork
    public Role find(@PathParam("id") LongParam id) {
        return roleDAO.findById(id.get());
    }
    
    @GET
    @UnitOfWork
    public List<Role> list() {
        return roleDAO.findAll();
    }
    
    @DELETE
    @Path("{id}")   
    @UnitOfWork
    public void delete(@PathParam("id") LongParam id) {
    	roleDAO.delete(id.get());
    }
}
